package com.wbao.dianniu.tabstrip.fragments;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.lhh.apst.library.CustomPagerSlidingTabStrip2;
import com.lhh.apst.library.ViewHolder;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.ui.AmQuestionActivity;
import com.wbao.dianniu.ui.AmSearchActivity;
import com.wbao.dianniu.ui.AskActivity;
import com.wbao.dianniu.ui.QaSearchActivity;
import com.wbao.dianniu.R;
import com.wbao.dianniu.update.ErrorDialogHelper;

/**
 * 电纽问答
 * 
 * @author yejianjun
 * 
 */
public class FirstFragment extends Fragment implements OnPageChangeListener {
	public CustomPagerSlidingTabStrip2 mAPSTS;
	public APSTSViewPager mVP;

	private static final int VIEW_QA = 0;
	private static final int VIEW_ANONY = 1;

	private static final int VIEW_SIZE = 2;

	private QAFragment qaFragment = null;
	private AmFragment amFragment = null;

	private FragmentAdapter adapter;
	private ImageButton searchBtn;
	private ImageButton releaseBtn;
	private Dialog dialog;
	private int CurrentTab = 0;

	public static FirstFragment instance(Context context) {
		FirstFragment view = new FirstFragment();
		return view;

	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_fist_pager, null);
		searchBtn = (ImageButton) view.findViewById(R.id.search_button);
		releaseBtn = (ImageButton) view.findViewById(R.id.release_button);
		mAPSTS = (CustomPagerSlidingTabStrip2) view.findViewById(R.id.fistview_tabs);
		mVP = (APSTSViewPager) view.findViewById(R.id.fistview_apsts);
		mVP.setOffscreenPageLimit(VIEW_SIZE);

		mVP.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		mAPSTS.setIndicatorHeight(4);
		mAPSTS.setViewPager(mVP);
		mAPSTS.setOnPageChangeListener(this);
		searchBtn.setOnClickListener(clickListener);
		releaseBtn.setOnClickListener(clickListener);
		return view;
	}

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.search_button:
				Intent intent = new Intent();
				if(VIEW_ANONY == CurrentTab){
					intent.setClass(getActivity(), AmSearchActivity.class);
				}else{
					intent.setClass(getActivity(), QaSearchActivity.class);
				}
				startActivity(intent);
				break;
			case R.id.release_button: // 提问按钮
//				showRealase();
				if(VIEW_ANONY == CurrentTab){// 当前处于匿名页 跳转到匿名提问 
					Intent anonymous = new Intent();
					anonymous.setClass(getActivity(), AmQuestionActivity.class);
					startActivity(anonymous);
				}else{
					if (checkLevel()) {
						Intent ask = new Intent();
						ask.setClass(getActivity(), AskActivity.class);
						startActivity(ask);
					} else {
						ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
					}
				}
				break;
			case R.id.custom_qa_btn:
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				if (checkLevel()) {
					Intent ask = new Intent();
					ask.setClass(getActivity(), AskActivity.class);
					startActivity(ask);
				} else {
					ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
				}
				break;
			case R.id.custom_am_btn:
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				Intent anonymous = new Intent();
				anonymous.setClass(getActivity(), AmQuestionActivity.class);
				startActivity(anonymous);
				break;
			case R.id.close_Dialog:
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				break;
			default:
				break;
			}
		}
	};

	public boolean checkLevel() {
		boolean ret = false;
		if (GlobalContext.getPerfectData()) {
			// 提示用户完成认证 必须达到一级认证才能提问
			ret = true;
		}
		return ret;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		adapter = new FragmentAdapter(getFragmentManager());
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	private class FragmentAdapter extends FragmentStatePagerAdapter implements
			CustomPagerSlidingTabStrip2.CustomTabProvider {
		protected LayoutInflater mInflater;
		
		public FragmentAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
			mInflater = LayoutInflater.from(getActivity());
		}


		@Override
		public Fragment getItem(int position) {
			// TODO Auto-generated method stub
			if (position >= 0 && position < VIEW_SIZE) {
				switch (position) {
				case VIEW_QA:
					if (null == qaFragment)
						qaFragment = QAFragment.instance();
					return qaFragment;

				case VIEW_ANONY:
					if (null == amFragment)
						amFragment = AmFragment.instance();
					return amFragment;
				default:
					break;
				}
			}

			return null;
		}

		@Override
		public int getCount() {
			return VIEW_SIZE;
		}

		@Override
		public View getSelectTabView(int position, View convertView) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.custom_select_tab,
						null);
			}
			TextView tv = ViewHolder.get(convertView, R.id.tvTab);
			tv.setText(getPageTitle(position));
			tv.setTextColor(getResources().getColor(R.color.white));
			return convertView;
		}

		@Override
		public View getDisSelectTabView(int position, View convertView) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.custom_disselect_tab,null);
			}
			TextView tv = ViewHolder.get(convertView, R.id.tvTab);
			tv.setText(getPageTitle(position));
			tv.setTextColor(getResources().getColor(R.color.public_bg));
			return convertView;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case VIEW_QA:
				return getResources().getString(R.string.dn_QandA);
			case VIEW_ANONY:
				return getResources().getString(R.string.anonymous_title);
			default:
				break;
			}

			return super.getPageTitle(position);
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int arg0) {
		CurrentTab = arg0;
	}
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onPageStart("FirstFragment"); //统计页面，"MainScreen"为页面名称，可自定义
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPageEnd("FirstFragment"); 
	}
}
